package com.cskaoyan.javase.exception._1handle._4throw;

/**
 * @description: 用抛出异常来写get/set方法
 * @author: wuguidong@cskaoyan.onaliyun.com
 **/

public class Demo2 {
    public static void main(String[] args) {
        Student s = new Student();
        try {
            s.setAge(120);
        } catch (IllegalArgumentException e) {
            //往前端发消息,告诉用户修改输入
            e.printStackTrace();
        }
        System.out.println(s.getAge());
    }
}
class Student {
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        //需要传入的age在1到150之间
        if (age < 1) throw new IllegalArgumentException("你不可能没有出生!");
        if (age > 150) throw new IllegalArgumentException("您在哪里修仙呢?");
        this.age = age;
    }
}